package cn.edu.lcvc.ebuy_2023rj2.model.common;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.List;

/**
 * 分页类，用于返回前端
 */
public class Pagination <T>{
    private int totalRecords;//总共记录数
    private  Integer maxPage;//最大值
    private List<T> data;//当前页的记录数
    private Integer pageSize;//每页显示的记录数
    private Integer page;//显示当前页
    @JsonIgnore//json不返回该属性
private  int offset;//当前页起始记录位置（用语数据库）

    public Pagination() {
    }

    public Pagination( Integer page, Integer pageSize,int totalRecords) {
        if (page==null){
            page=1;
        }
        if (pageSize==null){
            pageSize=10;
        }
        int maxPage=totalRecords%pageSize==0?totalRecords/pageSize:totalRecords/pageSize+1;//计算出最大页
        if (page<1){
            page=1;
        }else if (page>maxPage){//：如果显示的页面超出范围，则显示最后一页
            page=maxPage;
        }

        this.page = page;
        this.pageSize = pageSize;
        this.totalRecords = totalRecords;
     this.maxPage=maxPage;
     this.offset=(page-1)*pageSize;

    }


    public int getTotalRecords() {
        return totalRecords;
    }


    public Integer getMaxPage() {
        return maxPage;
    }



    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Integer getPageSize() {
        return pageSize;
    }



    public Integer getPage() {
        return page;
    }


    public int getOffset() {
        return offset;
    }


}
